import 'package:bruno/bruno.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_boost/flutter_boost.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:xiongmao_clean_flutter_module/business/common/workpost/bean/work_rules_data_entity.dart';
import 'package:xiongmao_clean_flutter_module/business/common/workpost/iview/work_rules_save_iview.dart';
import 'package:xiongmao_clean_flutter_module/business/common/workpost/presenter/work_post_add_edit_persenter.dart';
import 'package:xiongmao_clean_flutter_module/util/log_utils.dart';

import '../../../../mvp/base_page.dart';
import '../../../../mvp/power_presenter.dart';
import '../../../../res/colors.dart';
import '../../../../widgets/custom_sing_column_picker.dart';
import '../controller/work_rules_controller.dart';
import '../iview/work_post_save_iview.dart';
import '../presenter/work_rules_persenter.dart';

/**
 * 用工规则，直接写这了
 */
class WorkRulesPage extends StatefulWidget {
  @override
  _WorkRulesPageState createState() => _WorkRulesPageState();
}

class _WorkRulesPageState extends State<WorkRulesPage> with BasePageMixin<WorkRulesPage, PowerPresenter<dynamic>>, AutomaticKeepAliveClientMixin<WorkRulesPage> implements WorkRulesSaveIView {
  WorkRulesPagePresenter? _presenter;

  final WorkRulesPageController _controller = WorkRulesPageController();

  var _index = 0; //记录当前选中的下标内容
  var _dateIndex = 0; //记录薪水结算日选中的下标内容

  final List<String> _list = [
    '50岁',
    '51岁',
    '52岁',
    '53岁',
    '54岁',
    '55岁',
    '56岁',
    '57岁',
    '58岁',
    '59岁',
    '60岁',
    '61岁',
    '62岁',
    '63岁',
    '64岁',
    '65岁',
    '66岁',
    '67岁',
    '68岁',
    '69岁',
    '70岁',
  ];

  final List<String> _dateList = [
    '1日',
    '2日',
    '3日',
    '4日',
    '5日',
    '6日',
    '7日',
    '8日',
    '9日',
    '10日',
    '11日',
    '12日',
    '13日',
    '14日',
    '15日',
    '16日',
    '17日',
    '18日',
    '19日',
    '20日',
    '21日',
    '22日',
    '23日',
    '24日',
    '25日',
    '26日',
    '27日',
    '28日',
  ];

  @override
  void initState() {
    super.initState();
    _presenter?.getWorkRulesInfo();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: BrnAppBar(
          title: '用工规则',
          backLeadCallback: () {
            BoostNavigator.instance.pop();
          }),
      backgroundColor: Colours.base_primary_bg_page,
      body: Container(
        margin: EdgeInsets.only(top: 10),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            ///用工年龄上线
            Container(
              padding: EdgeInsets.only(left: 16, right: 16, top: 10, bottom: 10),
              color: Colors.white,
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start, // 设置顶部对齐
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        "用工年龄上限",
                        style: TextStyle(
                          fontSize: 14,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      Text(
                        "超过上限的项目成员，将无法办理入职",
                        style: TextStyle(fontSize: 12, color: Colors.grey),
                      ),
                    ],
                  ),
                  Obx(() => InkWell(
                        onTap: () {
                          SingleColumnDataPickerView.showSingleColumnDataPicker(context, '用工年龄上限', _list, _index, (index, selectedText) {
                            print('当前选择的下标：$index');
                            print('当前选择的文本内容：$selectedText');
                            _index = index;
                            _controller.updateMaxAge(selectedText.replaceAll("岁", ""));
                            saveWorkRules();
                          });
                        },
                        child: Wrap(
                          children: [
                            Text(
                              "${_controller.maxAge}岁",
                              style: TextStyle(fontSize: 14, color: Colors.black),
                            ),
                            Image.asset(
                              'assets/images/base/icon_base_gray_arrow.png',
                              width: 20,
                              height: 20,
                            ),
                          ],
                        ),
                      )),
                ],
              ),
            ),

            ///不允许补录之前的信息
            Container(
              padding: EdgeInsets.only(left: 16, right: 16, top: 10, bottom: 10),
              color: Colors.white,
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start, // 设置顶部对齐
                children: [
                  Expanded(
                      child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        "不允许补录今日之前的入职信息",
                        style: TextStyle(
                          fontSize: 14,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      Text(
                        "开启后入职时间将不能选到今日之前的时间",
                        style: TextStyle(fontSize: 12, color: Colors.grey),
                      ),
                    ],
                  )),
                  Obx(() => BrnSwitchButton(
                        size: Size(40, 20),
                        value: _controller.isEntryBeforeToday.value,
                        // 使用新变量来表示Switch的状态
                        onChanged: (bool value) {
                          // 更新Switch的状态
                          _controller.isEntryBeforeToday.value = value;
                          saveWorkRules();
                        },
                      )),
                ],
              ),
            ),

            ///拍照上传。无犯罪证明
            Container(
              padding: EdgeInsets.only(left: 16, right: 16, top: 10, bottom: 10),
              color: Colors.white,
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start, // 设置顶部对齐
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        "拍照上传“无犯罪证明”",
                        style: TextStyle(
                          fontSize: 14,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      Text(
                        "关闭后，入职时将不显示“无犯罪证明”一栏",
                        style: TextStyle(fontSize: 12, color: Colors.grey),
                      ),
                    ],
                  ),
                  Obx(() => BrnSwitchButton(
                        size: Size(40, 20),
                        value: _controller.isUploadNotGuilty.value,
                        // 使用新变量来表示Switch的状态
                        onChanged: (bool value) {
                          // 更新Switch的状态
                          _controller.isUploadNotGuilty.value = value;
                          saveWorkRules();
                        },
                      )),
                ],
              ),
            ),

            ///办理入职需要入职员工签字
            Container(
              padding: EdgeInsets.only(left: 16, right: 16, top: 10, bottom: 10),
              color: Colors.white,
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start, // 设置顶部对齐
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        "办理入职时需要入职的员工签字确认",
                        style: TextStyle(
                          fontSize: 14,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      Text(
                        "入职时若签署签字合同则无需该步骤",
                        style: TextStyle(fontSize: 12, color: Colors.grey),
                      ),
                    ],
                  ),
                  Obx(() => BrnSwitchButton(
                        size: Size(40, 20),
                        value: _controller.isEntrySign.value,
                        // 使用新变量来表示Switch的状态
                        onChanged: (bool value) {
                          // 更新Switch的状态
                          _controller.isEntrySign.value = value;
                          saveWorkRules();
                        },
                      )),
                ],
              ),
            ),

            ///发薪日结算
            Container(
              padding: EdgeInsets.only(left: 16, right: 16, top: 10, bottom: 10),
              color: Colors.white,
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start, // 设置顶部对齐
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        "薪资结算日",
                        style: TextStyle(
                          fontSize: 14,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      Text(
                        "入职合同时，该日期可自动填写到合同中",
                        style: TextStyle(fontSize: 12, color: Colors.grey),
                      ),
                    ],
                  ),
                  Obx(() => InkWell(
                        onTap: () {
                          SingleColumnDataPickerView.showSingleColumnDataPicker(context, '用工年龄上限', _dateList, _dateIndex, (index, selectedText) {
                            print('当前选择的下标：$index');
                            print('当前选择的文本内容：$selectedText');
                            _dateIndex = index;
                            _controller.updateSalaryDate(selectedText.replaceAll("日", ""));
                            saveWorkRules();
                          });
                        },
                        child: Wrap(
                          children: [
                            Text(
                              "${_controller.salaryDate}日",
                              style: TextStyle(fontSize: 14, color: Colors.black),
                            ),
                            Image.asset(
                              'assets/images/base/icon_base_gray_arrow.png',
                              width: 20,
                              height: 20,
                            ),
                          ],
                        ),
                      )),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  saveWorkRules() {
    _presenter?.updateWorkRules(_controller.maxAge.toString(), _controller.isEntryBeforeToday.value ? "1" : "2", _controller.isUploadNotGuilty.value ? "1" : "2", _controller.isEntrySign.value ? "1" : "2", _controller.salaryDate.toString());
  }

  @override
  PowerPresenter createPresenter() {
    final PowerPresenter<dynamic> powerPresenter = PowerPresenter<dynamic>(this);
    _presenter = WorkRulesPagePresenter(_controller);
    powerPresenter.requestPresenter([_presenter!]);
    return powerPresenter;
  }

  @override
  bool get wantKeepAlive => false;

  @override
  workRulesSaveStatus() {
    //重新那一边数据
    _presenter?.getWorkRulesInfo();
  }
}
